package cn.itlaobing.web.servlet;

import cn.itlaobing.exception.BusinessException;
import cn.itlaobing.model.UserModel;
import cn.itlaobing.service.UserService;
import cn.itlaobing.service.impl.UserServiceImpl;
import cn.itlaobing.web.Constants;
import cn.itlaobing.web.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Administrator on 2017/11/3.
 */
@WebServlet("/auth/*")
public class AuthenticationServlet extends BaseServlet {

    @RequestMapping("/signin")
    public void signin(HttpServletRequest res, HttpServletResponse resp) throws BusinessException, ServletException, IOException {
        String username=res.getParameter("username");
        String password=res.getParameter("password");
        UserService userService=new UserServiceImpl();
        UserModel userModel=userService.authentication(username,password);
        if(userModel==null){

            res.setAttribute("MSG","用户名或密码错误！");
            res.getRequestDispatcher("/index.jsp").forward(res,resp);
            return;
        }
        //登录成功后将用户加到session中，重定向到
        res.getSession().setAttribute("USER_IN_SESSIONSCOPE",userModel);
        resp.sendRedirect(res.getContextPath()+"/orders/show");
    }
}
